package com.goldgov.branchCircle.service.impl;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.goldgov.branchCircle.dao.BranchCircleDao;
import com.goldgov.branchCircle.dao.MessageDao;
import com.goldgov.branchCircle.dao.ReplyMessageDao;
import com.goldgov.branchCircle.dao.ThumbUpDao;
import com.goldgov.branchCircle.service.BranchCircle;
import com.goldgov.branchCircle.service.BranchCircleQuery;
import com.goldgov.branchCircle.service.BranchCircleService;
import com.goldgov.branchCircle.service.Message;
import com.goldgov.branchCircle.service.OrganizationEntity;
import com.goldgov.branchCircle.service.ReplyMessage;
import com.goldgov.branchCircle.service.ThumbUp;
import com.goldgov.meetingstatistics.bean.MeetingOrg;
import com.goldgov.meetingstatistics.bean.MeetingOrgQuery;
import com.goldgov.utils.DateUtil;
import com.goldgov.utils.PropertyUtil;
@Service
public class BranchCircleServiceImpl implements BranchCircleService{

	@Autowired
	private BranchCircleDao branchCircleDao;
	
	@Autowired
	private ThumbUpDao thumbUpDao;
	
	@Autowired
	private MessageDao messageDao;
	
	@Autowired
	private ReplyMessageDao replyMessageDao;
	
	@Override
	public List<BranchCircle> getBranchCircleList(BranchCircleQuery query) {
		List<BranchCircle> list=branchCircleDao.getBranchCircleList(query);
		if(null!=list && list.size()>0) {
			for(BranchCircle b:list) {
				List<ThumbUp> tlist=thumbUpDao.getThumbUpList(b.getBranchID());
				b.setThumbUpList(tlist);
				List<Message> mlist=messageDao.getMessageList(b.getBranchID());
				b.setMessageList(mlist);
				if(null!=mlist && mlist.size()>0) {
					for(Message m:mlist) {
						List<ReplyMessage> rlist=replyMessageDao.getReplyMessageList(m.getMessageID());
						m.setReplyList(rlist);
					}
				}
			}
		}
		return list;
	}

	
	/**
	 * 根据组织id获取支部圈列表
	 * @param query
	 * @return
	 */
	public List<BranchCircle> getBranchCircleListByOrgId(BranchCircleQuery query){
		List<BranchCircle> list=branchCircleDao.getBranchCircleListByOrgId(query);
		if(null!=list && list.size()>0) {
			for(BranchCircle b:list) {
				List<ThumbUp> tlist=thumbUpDao.getThumbUpList(b.getBranchID());
				b.setThumbUpList(tlist);
				List<Message> mlist=messageDao.getMessageList(b.getBranchID());
				b.setMessageList(mlist);
				if(null!=mlist && mlist.size()>0) {
					for(Message m:mlist) {
						List<ReplyMessage> rlist=replyMessageDao.getReplyMessageList(m.getMessageID());
						m.setReplyList(rlist);
					}
				}
			}
		}
		return list;
	}
	
	
	@Override
	public BranchCircle getBranchCircle(String id) {
		return branchCircleDao.getBranchCircle(id);
	}
	@Override
	public BranchCircle getBranchCircleById(String id) {
		return branchCircleDao.getBranchCircleById(id);
	}

	@Override
	public Boolean addBranchCircle(BranchCircle branchCircle) {
		Boolean fase = false;
		int i = branchCircleDao.addBranchCircle(branchCircle);
		if (i >= 1) {
			fase = true;
		}
		return fase;
	}

	@Override
	public BranchCircle getBranchID() {
		return branchCircleDao.getBranchID();
	}

	@Override
	public void deleteBranchCircle(String branchID) {
		branchCircleDao.deleteBranchCircle(branchID);
	}

	@Override
	public List<BranchCircle> getBranchCircleListByAdmin(BranchCircleQuery query) {
		return branchCircleDao.getBranchCircleListByAdmin(query);
	}

	@Override
	public String getDutyByID(String orgID) {
		return branchCircleDao.getDutyByID(orgID);
	}

	@Override
	public List<BranchCircle> countBranchCircleByID(BranchCircleQuery query) {
		return branchCircleDao.countBranchCircleByID(query);
	}

	@Override
	public List<BranchCircle> countBranchCircleByHour(BranchCircleQuery query) {
		return branchCircleDao.countBranchCircleByHour(query);
	}

	@Override
	public List<BranchCircle> findBranchCircleByPerson(BranchCircleQuery query) throws Exception{
		//查询支部里的所有人
		List<BranchCircle> userList=branchCircleDao.findBranchOrgUser(query.getSearchOrgID());
		if(userList!=null&&userList.size()>0) {
			List<BranchCircle> list=branchCircleDao.findBranchCircleByPerson(query);
			for(BranchCircle userBean:userList) {
				for(BranchCircle bean:list) {
					if(userBean.getUserID().equals(bean.getUserID())) {
						BeanUtils.copyProperties(userBean,bean );
						break;
					}
				}
			}
		}
		return userList;
	}

	@Override
	public List<BranchCircle> findBranchCircleByPersonWeek(BranchCircleQuery query) {
		return branchCircleDao.findBranchCircleByPersonWeek(query);
	}

	@Override
	public List<BranchCircle> findBranchCircleByOrgAvg(BranchCircleQuery query) {
		
		return branchCircleDao.findBranchCircleByOrg(query);
	}

	@Override
	public BranchCircle findOrgByUserId(String userId) {
		return branchCircleDao.findOrgByUserId(userId);
	}

	@Override
	public List<BranchCircle> findMyBranchCirclWeek(BranchCircleQuery query) throws Exception {
		//查询支部里的所有人
		List<BranchCircle> userList=branchCircleDao.findBranchOrgUser(query.getSearchOrgID());
		if(userList!=null&&userList.size()>0) {
			List<BranchCircle> list=branchCircleDao.findMyBranchCirclWeek(query);
			for(BranchCircle userBean:userList) {
				for(BranchCircle bean:list) {
					if(userBean.getUserID().equals(bean.getUserID())) {
						BeanUtils.copyProperties(userBean,bean );
						break;
					}
				}
			}
		}
		return userList;
	}
	
	@Override
  public List<OrganizationEntity> findChildOrganization(String id){
	return branchCircleDao.findChildOrganization(id);
  }


	@Override
	public List<BranchCircle> countOrgBranchByTime(BranchCircleQuery query) {
		return branchCircleDao.countOrgBranchByTime(query);
	};
	
	public Boolean updateBranchCircleById(BranchCircle branchCircle) {
		Boolean fase = false;
		int i = branchCircleDao.updateBranchCircleById(branchCircle);
		if (i >= 1) {
			fase = true;
		}
		return fase;
	}
	
	/**
	 * 统计支部圈活跃度
	 * @date 2018年11月27日 下午2:47:40 
	 * @author hanxiangyu
	 * @param query
	 * @return
	 */
	public List<BranchCircle> findLivenessStatistics(BranchCircleQuery query){
		List<BranchCircle> list = new ArrayList<BranchCircle>();
		
		if (PropertyUtil.objectNotEmpty(query.getSearchDateType()) ) {
			if (query.getSearchDateType().intValue() == 5) {
				list = branchCircleDao.findLivenessStatisticsByYear(query);
			}else {
				BranchCircle branchCircle = branchCircleDao.findLivenessStatistics(query);
				if (query.getSearchDateType().intValue() == 3) {
					list.add(new BranchCircle(1, branchCircle.getCount1()));
					list.add(new BranchCircle(2, branchCircle.getCount2()));
					list.add(new BranchCircle(3, branchCircle.getCount3()));
					list.add(new BranchCircle(4, branchCircle.getCount4()));
					list.add(new BranchCircle(5, branchCircle.getCount5()));
					list.add(new BranchCircle(6, branchCircle.getCount6()));
					list.add(new BranchCircle(7, branchCircle.getCount7()));
					list.add(new BranchCircle(8, branchCircle.getCount8()));
					list.add(new BranchCircle(9, branchCircle.getCount9()));
					list.add(new BranchCircle(10, branchCircle.getCount10()));
					list.add(new BranchCircle(11, branchCircle.getCount11()));
					list.add(new BranchCircle(12, branchCircle.getCount12()));
				}else if (query.getSearchDateType().intValue() == 4) {
					list.add(new BranchCircle(1, branchCircle.getQuarter1()));
					list.add(new BranchCircle(2, branchCircle.getQuarter2()));
					list.add(new BranchCircle(3, branchCircle.getQuarter3()));
					list.add(new BranchCircle(4, branchCircle.getQuarter4()));
				}
			}
		}else {
			BranchCircle branchCircle = branchCircleDao.findLivenessStatistics(query);
			list.add(new BranchCircle(1, branchCircle.getCount1()));
			list.add(new BranchCircle(2, branchCircle.getCount2()));
			list.add(new BranchCircle(3, branchCircle.getCount3()));
			list.add(new BranchCircle(4, branchCircle.getCount4()));
			list.add(new BranchCircle(5, branchCircle.getCount5()));
			list.add(new BranchCircle(6, branchCircle.getCount6()));
			list.add(new BranchCircle(7, branchCircle.getCount7()));
			list.add(new BranchCircle(8, branchCircle.getCount8()));
			list.add(new BranchCircle(9, branchCircle.getCount9()));
			list.add(new BranchCircle(10, branchCircle.getCount10()));
			list.add(new BranchCircle(11, branchCircle.getCount11()));
			list.add(new BranchCircle(12, branchCircle.getCount12()));
		}
		
		return list;
	}


	@Override
	public List<BranchCircle> getBranchCircleOrgRank(BranchCircleQuery query) {
		return branchCircleDao.getBranchCircleOrgRank(query);
	}


	@Override
	public List<BranchCircle> countChildOrgBranch(BranchCircleQuery query) {
		List<BranchCircle> list=branchCircleDao.countChildOrgBranch(query);
		if(Integer.valueOf(2).equals(query.getSearchOrderType())) {
			List<BranchCircle> list2=new ArrayList<>();
				//根据avgReachNum从大到小排序
				list2=list.stream()
						.sorted(Comparator.comparing(BranchCircle::getArticleNum).reversed())
						.collect(Collectors.toList());
			return list2;
		}
		return list;
	}
	/**
	 * 获取列表
	 * @date 2018年12月14日 下午3:21:21 
	 * @author hanxiangyu
	 * @param query
	 * @return
	 */
	public List<MeetingOrg> findOrgBranchCSList(MeetingOrgQuery query){
		if (PropertyUtil.objectNotEmpty(query.getSearchType())) {
			switch (query.getSearchType()) {
			case 3:
				query.setSearchYear(String.valueOf(DateUtil.getYear(new Date())));
				break;
			case 4:
				query.setSearchYear(String.valueOf(DateUtil.getYear(new Date())));
				break;
			}
		}
		return branchCircleDao.findOrgBranchCSList(query);
	}
}
